package com.houseman.workFlow.dao.impl;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.ArrayList;
import java.util.List;

import com.houseman.workFlow.bean.WorkFlowFolder;
import com.houseman.workFlow.dao.FolderDao;
import com.houseman.workFlow.utils.DBUtils;

public class FolderDaoImpl implements FolderDao{

	/**
	 * 获得所有文件夹集合
	 * @return
	 * @throws Exception
	 */
	public List<WorkFlowFolder> queryFolders() throws Exception {
		//获得连接
		Connection conn = DBUtils.geConnection();
		
		PreparedStatement ps = conn.prepareStatement("SELECT FOLDERID,NAME FROM WORKFLOWFOLDER");
		ResultSet rs = ps.executeQuery();

		List<WorkFlowFolder> folders = new ArrayList<WorkFlowFolder>();
		while (rs.next()) {
			WorkFlowFolder wf = new WorkFlowFolder();
			wf.setFolderId(rs.getInt("FOLDERID"));
			wf.setName(rs.getString("NAME"));
			
			folders.add(wf);
		}
		
		//关闭连接
		DBUtils.close(conn, ps, rs);
		return folders;
	}
	
	/**
	 * 保存文件夹信息
	 * @param folder
	 * @throws Exception
	 */
	public void saveFolder(WorkFlowFolder folder) throws Exception {
		//获得连接
		Connection conn = DBUtils.geConnection();
		
		//insert workFlowGraph
		PreparedStatement ps = conn.prepareStatement("INSERT INTO WORKFLOWFOLDER(NAME) VALUES(?)");
		ps.setString(1, folder.getName());
		ps.execute(); //新增 删除
		
		//关闭连接
		DBUtils.close(conn, ps);
	}
}
